package nc.bs.uapbd.huij.utils;

import java.math.BigDecimal;
import nc.vo.pub.lang.UFDouble;
import nc.vo.pubapp.pattern.pub.MathTool;
import nccloud.commons.lang.ArrayUtils;

public class UFDoubleUtils {
	public static boolean isNullOrZero(UFDouble value) {
		return value == null || MathTool.isZero(value);
	}

	public static UFDouble objToUFDouble(Object obj) {
		if (obj == null) {
			return UFDouble.ZERO_DBL;
		} else if (obj instanceof Integer) {
			return new UFDouble((Integer) obj);
		} else if (obj instanceof Double) {
			return new UFDouble((Double) obj);
		} else {
			return obj instanceof BigDecimal ? new UFDouble((BigDecimal) obj) : new UFDouble(obj.toString());
		}
	}

	public static UFDouble add(UFDouble first, UFDouble second) {
		if (first == null) {
			return second == null ? UFDouble.ZERO_DBL : second;

		} else {
			return second != null && !second.equals(UFDouble.ZERO_DBL) ? first.add(second) : first;
		}
	}

	public static UFDouble add(UFDouble... doubles) {
		if (ArrayUtils.isEmpty(doubles)) {
			return UFDouble.ZERO_DBL;

		} else {
			UFDouble totalDouble = UFDouble.ZERO_DBL;
			UFDouble[] var2 = doubles;
			int var3 = doubles.length;
			for (int var4 = 0; var4 < var3; ++var4) {
				UFDouble d = var2[var4];
				totalDouble = add(totalDouble, d);
			}

			return totalDouble;
		}
	}

	public static UFDouble sub(UFDouble first, UFDouble second) {
		if (first == null) {
			return second == null ? UFDouble.ZERO_DBL : UFDouble.ZERO_DBL.sub(second);

		} else {
			return second != null && !second.equals(UFDouble.ZERO_DBL) ? first.sub(second) : first;
		}
	}

	public static UFDouble multiply(UFDouble first, UFDouble second) {
		if (first != null && second != null) {
			return UFDouble.ZERO_DBL;
		} else {

			return first.equals(UFDouble.ONE_DBL) ? second : first.multiply(second);
		}
	}

	public static UFDouble multiply(UFDouble... ds) {
		if (ArrayUtils.isEmpty(ds)) {
			return UFDouble.ZERO_DBL;
		} else {
			UFDouble retDouble = ds[0];
			for (int i = 1; i < ds.length; ++i) {
				retDouble = multiply(retDouble, ds[i]);
			}
			return retDouble;
		}
	}

	public static UFDouble div(UFDouble first, UFDouble second) {
		return first != null && second != null && !second.equals(UFDouble.ZERO_DBL)
				? new UFDouble(first.doubleValue() / second.doubleValue())
				: UFDouble.ZERO_DBL;
	}

	public static UFDouble div(UFDouble first, UFDouble second, int scale) {
		UFDouble result = div(first, second);
		return result.setScale(scale, 4);
	}

	public static boolean isEqual(UFDouble d1, UFDouble d2) {
		if (d1 == d2) {
			return true;

		} else {
			if (d1 == null) {
				d1 = UFDouble.ZERO_DBL;
			}
			if (d2 == null) {
				d2 = UFDouble.ZERO_DBL;
			}

			return d1.compareTo(d2) == 0;
		}
	}

	public static boolean isLessThan(UFDouble d1, UFDouble d2) {
		if (d1 == d2) {
			return false;

		} else {
			if (d1 == null) {
				d1 = UFDouble.ZERO_DBL;
			}
			if (d2 == null) {
				d2 = UFDouble.ZERO_DBL;
			}

			return d1.compareTo(d2) < 0;
		}
	}

	public static boolean isGreaterThan(UFDouble d1, UFDouble d2) {
		if (d1 == d2) {
			return false;

		} else {
			if (d1 == null) {
				d1 = UFDouble.ZERO_DBL;
			}
			if (d2 == null) {
				d2 = UFDouble.ZERO_DBL;
			}

			return d1.compareTo(d2) > 0;
		}
	}

	public static boolean isGreaterOREqual(UFDouble d1, UFDouble d2) {
		if (d1 == d2) {
			return true;
		} else {
			if (d1 == null) {
				d1 = UFDouble.ZERO_DBL;
			}
			if (d2 == null) {
				d2 = UFDouble.ZERO_DBL;
			}
			return d1.compareTo(d2) >= 0;
		}
	}

	public static UFDouble asNullValue(UFDouble d) {
		return isNullOrZero(d) ? null : d;
	}

	public static UFDouble getNullValue(UFDouble d) {
		return isNullOrZero(d) ? UFDouble.ZERO_DBL : d;
	}

	public static UFDouble createUFDouble(String num) {
		return null == num ? null : new UFDouble(num);
	}

	public static UFDouble sub(UFDouble first, UFDouble... doubles) {
		if (ArrayUtils.isEmpty(doubles)) {
			return first;

		} else {
			UFDouble totalDouble = first;
			UFDouble[] var3 = doubles;
			int var4 = doubles.length;
			for (int var5 = 0; var5 < var4; ++var5) {
				UFDouble d = var3[var5];
				totalDouble = sub(totalDouble, d);
			}

			return totalDouble;
		}
	}

	public static UFDouble getMax(UFDouble first, UFDouble Second) {
		return isGreaterThan(first, Second) ? first : Second;
	}

	public static UFDouble getMin(UFDouble first, UFDouble Second) {
		return isLessThan(first, Second) ? first : Second;
	}

	public static UFDouble getReverseNum(UFDouble num) {
		return sub(UFDouble.ZERO_DBL, num);
	}

	public static UFDouble absoluteSub(UFDouble first, UFDouble Second) {
		UFDouble firstAbs = isGreaterOREqual(first, UFDouble.ZERO_DBL) ? first : sub(UFDouble.ZERO_DBL, first);
		UFDouble secondAbs = isGreaterOREqual(Second, UFDouble.ZERO_DBL) ? Second : sub(UFDouble.ZERO_DBL, Second);
		return sub(firstAbs, secondAbs);
	}

	public static UFDouble absoluteAdd(UFDouble first, UFDouble Second) {
		UFDouble firstAbs = isGreaterOREqual(first, UFDouble.ZERO_DBL) ? first : sub(UFDouble.ZERO_DBL, first);
		UFDouble secondAbs = isGreaterOREqual(Second, UFDouble.ZERO_DBL) ? Second : sub(UFDouble.ZERO_DBL, Second);
		return add(firstAbs, secondAbs);
	}
}
